/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.opgea.token.dao.AbstractDAO;
import com.opgea.token.dao.TokenDetailsDAO;
import com.opgea.token.domain.entities.TokenDetails;

/**
 * 
 * @author Ramesh
 */
@Repository
public class TokenDetailsDAOImpl extends AbstractDAO<TokenDetails> implements TokenDetailsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    /*
    @Override
    public TokenDetails create(TokenDetails tokenDetails) {
	sessionFactory.getCurrentSession().save(tokenDetails);
	return tokenDetails;
    }

    @Override
    public TokenDetails update(TokenDetails tokenDetails) {
	TokenDetails tDetails = (TokenDetails) sessionFactory
		.getCurrentSession().get(TokenDetails.class,
			tokenDetails.getId());
	tDetails.setName(tokenDetails.getName());
	tDetails.setMobileNo(tokenDetails.getMobileNo());
	tDetails.setDestination(tokenDetails.getDestination());
	tDetails.setTokenStatus(tokenDetails.getTokenStatus());
	sessionFactory.getCurrentSession().update(tDetails);
	return tDetails;
    }
    
    @Override
    public TokenDetails delete(Long tokenDetailsId) {
	TokenDetails tokenDetails = this.find(tokenDetailsId);
	sessionFactory.getCurrentSession().delete(tokenDetails);
	return tokenDetails;
    }
    */
    
    @Override
    public TokenDetails find(Long id) {
	Query query = sessionFactory.getCurrentSession()
		.getNamedQuery("TokenDetails.findById").setParameter("id", id);
	TokenDetails tokenDetails = (TokenDetails) query.uniqueResult();
	return tokenDetails;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TokenDetails> findAll() {
	Query query = sessionFactory.getCurrentSession().getNamedQuery(
		"TokenDetails.findAll");
	List<TokenDetails> tokens = query.list();
	return tokens;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TokenDetails> findAllByCompanyId(Long companyId) {
	Query query = sessionFactory.getCurrentSession()
		.getNamedQuery("TokenDetails.findAllByCompanyId")
		.setParameter("companyId", companyId);
	List<TokenDetails> tokens = query.list();
	return tokens;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TokenDetails> findAllByDestination(Long destinationId) {
	Query query = sessionFactory.getCurrentSession()
		.getNamedQuery("TokenDetails.findAllByDestination")
		.setParameter("destinationId", destinationId);
	List<TokenDetails> tokens = query.list();
	return tokens;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TokenDetails> findAllByDestination(Long companyId,
	    Long destinationId) {
	Query query = sessionFactory.getCurrentSession().createQuery(
		"SELECT t FROM TokenDetails t WHERE t.token.company.id = '"
			+ companyId + "' and t.destination.id = '"
			+ destinationId + "'");
	System.out.println("Query String: " + query.getQueryString());
	List<TokenDetails> tokens = query.list();
	return tokens;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TokenDetails> searchTokens(Long companyId, String searchKey,
	    String tokenDate) {
	Date today = Calendar.getInstance().getTime();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	String dateString = sdf.format(today);
	StringBuilder query = new StringBuilder();
	query.append(" SELECT t FROM TokenDetails t WHERE t.token.company.id = ");
	query.append(companyId);
	if (tokenDate != null && !tokenDate.equalsIgnoreCase("")) {
	    query.append(" AND t.token.tokenDate = ");
	    query.append("'");
	    query.append(tokenDate);
	    query.append("'");
	} else {
	    query.append(" AND t.token.tokenDate = ");
	    query.append("'");
	    query.append(dateString);
	    query.append("'");
	}
	if (searchKey != null) {
	    query.append(" AND t.metaData like ");
	    query.append("'%");
	    query.append(searchKey);
	    query.append("%'");
	}
	Query queryObject = sessionFactory.getCurrentSession().createQuery(
		query.toString());
	List<TokenDetails> tokenDetails = queryObject.list();
	return tokenDetails;
    }
}
